/*
 * Copyright (c) 2025 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */

#include <plat/config.h>
#include "uart_rk3588.h"

// Register offset
volatile struct uart_regs_t {
    uint32_t rbr;
    uint32_t ier;
    uint32_t fcr;
    uint32_t lcr;
    uint32_t mcr;
    uint32_t lsr;
    uint32_t msr;
    uint32_t spr;

    uint32_t mdr1;
    uint32_t reg9;
    uint32_t regA;
    uint32_t regB;
    uint32_t regC;
    uint32_t regD;
    uint32_t regE;
    uint32_t uasr;
    uint32_t scr;
    uint32_t ssr;
} *uart2_base;

#define UART_LSR_THRE   0x20    // transmit holding register Empty
#define UART_LSR_DR     0x01    // data ready (received data available)

static volatile struct uart_regs_t *uart = (struct uart_regs_t *)DEVICE_VBASE_UART;

void serial_putchar(const char c)
{
    while(0U == (uart->lsr & UART_LSR_THRE));
    uart->rbr = c;
}

int serial_getchar(void)
{
	while (0U == (uart->lsr & UART_LSR_DR));

	return uart->rbr;
}

void serial_init(void)
{
    return;
}
